package br.com.cadastro.service;

import java.util.ArrayList;
import java.util.List;

import br.com.cadastro.model.Honda;
import br.com.cadastro.model.Moto;
import br.com.cadastro.model.Suzuki;
import br.com.cadastro.model.Yamaha;
import br.com.caelum.vraptor.ioc.ApplicationScoped;
import br.com.caelum.vraptor.ioc.Component;

@Component
@ApplicationScoped
public class Motos {
	
	private List<Moto> motos = new ArrayList<Moto>();
	
	public Motos() {
		list();
	}
	
	private Moto fazMoto(String modelo){
		if (modelo.equals("Honda")) {
			return new Honda();
		} else if (modelo.equals("Yamaha")) {
			return new Yamaha();
		} else if (modelo.equals("Suzuki")) {
			return new Suzuki();
		} else {
			throw new IllegalArgumentException("Modelo Invalido");
		}
	}
	
	public void addMoto(Moto moto){
		Moto motoPraLista = fazMoto(moto.getMarca());
		motoPraLista.setId(moto.getId());
		motoPraLista.setMarca(moto.getMarca());
		motoPraLista.setAno(moto.getAno());
		motoPraLista.setCc(moto.getCc()+"cc");
		motoPraLista.setModelo(moto.getModelo());
		motoPraLista.setPreco(moto.getPreco());
		motos.add(motoPraLista);
	}
	
	public void removeMoto(Moto moto){
		motos.remove(moto);
	}
	
	public List<Moto> listaTodos(){
		return motos;
	}
	
	public Moto pesquisaMoto(int id, List<Moto> lista){
		Moto moto = new Moto();
		for (int i = 0; i < lista.size(); i++) {
			if(lista.get(i).getId() == id){
				moto = lista.get(i);
			}
		}
		return moto;
	}
	
	private void list(){
		Honda honda = new Honda();
		Yamaha yamaha = new Yamaha();

		honda.setId(1);
		honda.setMarca("Honda");
		honda.setCc("150cc");
		honda.setModelo("Titan");
		honda.setPreco(2000d);
		honda.setAno(2012);
		
		yamaha.setId(2);
		yamaha.setMarca("Yamaha");
		yamaha.setCc("125cc");
		yamaha.setModelo("YBR");
		yamaha.setPreco(2020d);
		yamaha.setAno(2012);
		
		motos.add(honda);
		motos.add(yamaha);
		
	}

}
